package com.ufm.security.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import com.ufm.security.dao.RolesMapper;
import com.ufm.security.pojo.RightsAndRoles;
import com.ufm.security.pojo.Roles;
import com.ufm.security.pojo.TablesAndRoles;
import com.ufm.security.service.IRolesService;
import com.ufm.syslog.pojo.Syslog;
import com.ufm.syslog.service.ISyslogService;
import com.ufm.user.pojo.User;
import com.ufm.user.service.IUserService;
@Service(value="rolesService")
public class RolesServiceImpl implements IRolesService {
	@Autowired
	private RolesMapper rolesMapper;
	@Autowired
	private ISyslogService syslogService;
	@Autowired
	private IUserService userService;
	
	
	@Override
	public List<Roles> findAll(Map params) {
		return rolesMapper.findAll(params);
	}

	@Override
	public List<Roles> findByUserId(int userId) {
		return rolesMapper.findByUserId(userId);
	}
	
	@Override
	public Roles findById(int userId) {
		return rolesMapper.findById(userId);
	}
	
	@Override
	public void delete(int userId) {
		rolesMapper.delete(userId);
	}
	

	
	@Override
	public void update(Roles roles) {
		rolesMapper.update(roles);
	}
	
	@Override
	public int countUserUsed(int id){
		return rolesMapper.countUserUsed(id);
	}
	
	@Override
	public void deleteMore(String[] ids,HttpServletRequest request) {
		for (int i = 0; i < ids.length; i++) {
			
			Roles roles = rolesMapper.findById(Integer.parseInt(ids[i]));
			
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User userSession = userService.findByName(userDetails.getUsername());
			Syslog syslog = new Syslog("角色维护", "删除角色:"+roles.getRole_name(), 1, userSession.getUserId(), userSession.getUserName(), request.getRemoteAddr(), new Date());
			syslogService.insert(syslog);
			rolesMapper.delete(Integer.parseInt(ids[i]));
			rolesMapper.deleteRolesRight(Integer.parseInt(ids[i]));
		}
	}
	
	
}
